package com.loyal.loyalerp.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.permission.mapper.GyPermissionMapper;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.model.vo.GridVO;
import com.loyal.loyalerp.system.mapper.GySystemMapper;
import com.loyal.loyalerp.system.model.GySystem;
import com.loyal.loyalerp.system.service.SystemService;

@Controller
@RequestMapping("/systemController")
public class SystemController {
	@Autowired
	private GySystemMapper systemMapper;
	@Autowired
	private GyPermissionMapper permissionMapper;
	@Autowired
	private SystemService systemService;
	//模糊查询
	@RequestMapping("/selectAll")
	public @ResponseBody Object selectAll(String _search, Integer rows, Integer page, String sord, String sidx, String searchField, String searchString) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
	
		if ("false".equals(_search) || StringUtils.isEmpty(_search))// 列表显示
		{
			PageInfo<GySystem> pageInfo = systemService.listGySystem(page, rows, sord, sidx);
			return GridVO.fromPageInfo(pageInfo);
		} 
		//模糊查询
		else {
			PageInfo<GySystem> pageInfo = systemService.findGySystem(page, rows, sord, searchField, searchString);
			return GridVO.fromPageInfo(pageInfo);
		}

		
	}
	
	//增删改操作
	@RequestMapping("/editSystem")
	@ResponseBody
	public Object edit(HttpServletRequest request,HttpServletResponse response,@Valid GySystem sys,String oper,String id) {
		System.out.println("nihao");
		
		Map<String, Object> map = new HashMap<String,Object>();
		System.out.println("oper:"+oper);
		if(oper.equals("add")) {
			int status = systemMapper.insert(sys);
			System.out.println("状态："+status);
			if(status == 1) {
				return ServerResponse.createBySuccess();
			}else {
				return ServerResponse.createByError();
			}
		}else if(oper.equals("edit")) {
			systemMapper.updateByPrimaryKey(sys);
			map.put("status", 200);
			return map;
		}else if(oper.equals("del")) {
			/**
			 * 先从premission表中删除systemCode对应的所有数据，
			 * 再删除system表中的数据
			 */
			Integer sid=Integer.parseInt(id);
			GySystem system=systemMapper.selectByPrimaryKey(sid);
			int rs=permissionMapper.deleteBySystemCode(system.getSystemcode());
			System.out.println("rs:"+rs);
			try {
				systemMapper.deleteByPrimaryKey(system.getId());
			}catch(Exception e) {
				e.printStackTrace();
			}
			return ServerResponse.createBySuccess();
		}
		return map;
		
	}
	/**
	 * 跳转到主页
	 * @return
	 */
	@RequestMapping("/system")
	public String  system() {
		return "/System/system";
	}
	/**
	 * 调到编辑界面
	 * @return
	 */
	@RequestMapping("/editjsp")
	public String  editjsp() {
		return "System/editSystem";
	}
	/**
	 * 调到搜索界面
	 * @return
	 */
	@RequestMapping("/searchjsp")
	public String searchjsp() {
		return "System/search";
	}
}
